<?php

class User_model extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	function create_user($response) {
		if(count($this->get_user($response['user_id'])) > 0 ){
			return true;
		}
	
		$sql = "INSERT INTO user (fb_id, email, last_newsletter, date_created) VALUES (?, ?, ?, NOW())";		
		$last_newsletter = date('Y-m-d H:i:s',time() - 6*24*60*60);
		$result = $this->db->query($sql, array($response['user_id'], $response['registration']['email'], $last_newsletter));
		return $result;
	}

	function get_user($fb_id) {
		$sql = "SELECT * FROM user WHERE fb_id = ? AND is_deleted = 0";
		$query = $this->db->query($sql, $fb_id);
			
		$data = array();
		if($query->num_rows() > 0){
			$data = $query->row_array();				
		}
		return $data;
	}
	
	function get_category_preference($fb_id) {
		$sql = "SELECT category.category_name, category.category_id 
				FROM category_preference, category 
				WHERE category_preference.fb_id = ? AND 
					  category_preference.category_id = category.category_id" ;
		$query = $this->db->query($sql, $fb_id);
		$data = array();
		if ($query->num_rows() > 0) {
			foreach ($query->result_array() as $row) {
				$data[] = $row;
			}
		}
		return $data;
	}
	
	function update_category_preference($fb_id,$category_list){
		$sql = "DELETE FROM category_preference WHERE fb_id = ?";
		$this->db->query($sql, $fb_id);

		if (count($category_list) > 0) {		
			foreach ($category_list as $cat) {
				$sql = "INSERT INTO category_preference (fb_id, category_id) VALUES (?, ?)";
				$this->db->query($sql, array($fb_id, $cat['category_id']));
			}
		}
	}
	
	function get_area_preference($fb_id) {
		$sql = "SELECT area.area_name, area.area_id 
				FROM area_preference, area 
				WHERE area_preference.fb_id = ? AND area_preference.area_id = area.area_id" ;
		$query = $this->db->query($sql, $fb_id);
		$data = array();
		if ($query->num_rows() > 0) {
			foreach ($query->result_array() as $row) {
				$data[] = $row;
			}
		}
		
		return $data;
	}
	
	function update_area_preference($fb_id,$area_list){
		$sql = "DELETE FROM area_preference WHERE fb_id = ?";
		$this->db->query($sql, $fb_id);

		if (count($area_list) > 0) {
			foreach ($area_list as $area) {
				$sql = "INSERT INTO area_preference (fb_id, area_id) VALUES (?, ?)";
				$this->db->query($sql, array($fb_id, $area['area_id']));
			}
		}
	}
	
	function get_area_group() {
		$query = $this->db->query("SELECT * FROM area_group");
		$data = array();
		if ($query->num_rows() > 0) {
			foreach ($query->result_array() as $row) {
				$data[] = $row;
			}
		}
		return $data ;
	}
	
	function get_area() {
		$query = $this->db->query("SELECT * FROM area");
		$data = array();
		if ($query->num_rows() > 0) {
			foreach ($query->result_array() as $row) {
				$data[] = $row;
			}
		}
		return $data ;
	}
	
	function update_profile($fb_id, $other ,$newsletter_day, $new_offer, $new_request, $new_comment, $new_notification) {
		$sql = "UPDATE user SET other = ?, newsletter_day = ?, new_offer = ?, new_request = ?,
				new_comment = ?, new_notification = ? WHERE fb_id = ?";
		$this->db->query($sql, array(strip_tags($other, $fb_id), $newsletter_day, $new_offer, 
									$new_request, $new_comment, $new_notification, $fb_id));
	}
	
	function get_total_users(){
		$sql = "SELECT * FROM user WHERE is_deleted != 1";
		$query = $this->db->query($sql);
		
		$data = $query->num_rows();
		return $data;
	}
	
	function update_last_newsletter($fb_id,$last_newsletter) {
		$sql = "UPDATE user SET last_newsletter = ? WHERE fb_id = ? ";
		$query = $this->db->query($sql,array($last_newsletter, $fb_id));
	}

	// These below functions are purely for testing. Please do not use it. Minh Tu
	/*
	function update_last_newsletter_zero() {
		$update_date = date("Y-h-d H:i:s", time() - 7*24*60*60);
		$sql = "UPDATE user SET last_newsletter = ?, newsletter_day = 7 WHERE newsletter_day = 0" ;
		$query = $this->db->query($sql,array($update_date));
	}*/
}
?>